<template>
  <div>
    <div class="operation">
      <el-button type="danger" plain @click="delBatch">批量删除</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" strip @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
        <el-table-column label="内容">
          <template #default="scope">
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <el-table-column prop="releaseTime" label="通知时间"></el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template v-slot="scope">
            <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Messages",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      ids: []
    }
  },
  created() {
    this.$request.put('/messages/read')  // 发起请求
    this.load(1)
  },
  methods: {
    load(pageNum) {
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/messages/selectPage',
          {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              userId:this.user.id
            }
          }).then(res => {
        this.tableData = res.data?.list
        this.total=res.data?.total
      })
    },
    handleCurrentChange(pageNum){
      this.load(pageNum)
    },
    handleSelectionChange(row) {   // 当前选中的所有的行数据
      this.ids = row.map(v => v.id)
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/messages/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success(res.msg)
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    delBatch() {   // 批量删除
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确定批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/messages/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success(res.msg)
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
  }
}
</script>

<style scoped>
.operation {
  margin-top: 5px;
  padding: 10px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1)
}
 .table {
   margin-top: 5px;
   padding: 15px 10px;
   background-color: #fff;
   border-radius: 5px;
   box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
 }
.manager-main-right .table .pagination {
  margin-top: 10px;
}
</style>